<?php

	class Book {

		var $id;			// STRING: Book identifikator
		var $header;		// ARRAY: Book headers
		var $book;			// ARRAY: display book
		var $content;		// STRING: Book content from DB
		var $chapter;		// ARRAY: Book chapter identifikators


		// Set book identificator, language, database
		public function Book($id) {
			$this->id = $id;
		}


		// Roman number converter
		private function numberToRoman($num)  {
			// Siguro perdorimin e vetem numrave te plote Integer
			$n = intval($num);
			$result = '';
			// Deklarimi i formave per konvertimin e numrave ne romak
			$lookup = array('M' => 1000, 'CM' => 900, 'D' => 500, 'CD' => 400,
							'C' => 100, 'XC' => 90, 'L' => 50, 'XL' => 40,
							'X' => 10, 'IX' => 9, 'V' => 5, 'IV' => 4, 'I' => 1);

			foreach ($lookup as $roman => $value)
			{
				// Determini i numrave
				$matches = intval($n / $value);
				// Ruaj numrin e  gjetur ne ndyshoren
				$result .= str_repeat($roman, $matches);
				// Substrakto numrin
				$n = $n % $value;
			}

			return $result;
		}


		// Set all book fields
		public function setBook() {

			$this->book = mysql_fetch_assoc(mysql_query("SELECT * FROM book WHERE book_id='".$this->id."'"));
			$this->book['book_section'] = implode(mysql_fetch_assoc(mysql_query("SELECT section_name FROM section WHERE section_id = '".$this->book['book_section_id']."'")));
			$this->book['book_author'] = implode(mysql_fetch_assoc(mysql_query("SELECT author_name FROM author WHERE author_id = '".$this->book['book_author_id']."'")));
		}


		// Set book content
		public function setContent($part,$activity) {

			$this->content = implode(mysql_fetch_assoc(mysql_query("SELECT book_content_content FROM book_content WHERE book_content_book_id='".$this->id."'")));

			// Pagebreaks in content
			$pagebreak = strpos($this->content,"<h1>");

			if ($pagebreak !== false) {

				// Chapter current
				if ($part == NULL)
					// If chapter isn't on top of content 
					$this->chapter['current'] = ($pagebreak > 0) ? 0 : 1;
				else 
					$this->chapter['current'] = $part;
					
				// Set book chapters
				$this->setHeaders();

				// Current chapter name
				$this->book['chapter'] = $this->header[$this->chapter['current']];
				
				// Chapter caption
				if ($this->chapter['current'] > 0)
					$this->book['caption'] = "|chapter| ".$this->numberToRoman($this->chapter['current']);
				else 	
					$this->book['caption'] = null;


				// Explode content in chapters
				$bookarray =  preg_split('|<h1>.*</h1>|im', $this->content);
				$this->book['content'] = $bookarray[$this->chapter['current']];

				// Count chapters
				$this->chapter['total'] = count($this->header);
				
				// If chapter isn't on top of content, don't set the  intro part like chapter
				if ($pagebreak > 0) 
					$this->chapter['total'] = $this->chapter['total'] - 1;

				// Chapter back
				if ($this->chapter['current'] == 0) {
					$this->chapter['back'] = "<div style='float:left; width:20%; color:#bbb;'>|intro|</div>";
					$this->chapter['index'] = "<div style='float:left; width:60%; text-align:center'>|intro|</div>";
				}
				elseif($this->chapter['current'] < 2) {
						$back = ($pagebreak > 0) ? "<a href='content.php?id=".$this->id."&part=0'>&lt; |intro|</a>" : "|chapter| ".$this->numberToRoman($this->chapter['current']);
						$this->chapter['back'] = "<div style='float:left; width:20%; color:#bbb;'>".$back."&nbsp;</div>";
						$this->chapter['index'] = "<div style='float:left; width:60%; text-align:center'>|chapter| ".$this->numberToRoman($this->chapter['current'])."</div>";
				}
				else {
					$back = $this->chapter['current'] - 1;
					$this->chapter['back'] = "<div style='float:left; width:20%;'><a href='content.php?id=".$this->id."&part=$back'>&lt; |chapter| ".$this->numberToRoman($back)."</a></div>";
					$this->chapter['index'] = "<div style='float:left; width:60%; text-align:center'>|chapter| ".$this->numberToRoman($this->chapter['current'])."</div>";
				}

				// Chapter forward
				if ($this->chapter['current'] < $this->chapter['total']) {
					$forward = $this->chapter['current'] + 1;
					$this->chapter['forward'] = "<div style='float:right; width:20%; text-align:right;'><a href='content.php?id=".$this->id."&part=$forward'>|chapter| ".$this->numberToRoman($forward)." &gt;</a></div>";
				}
				else {
					$this->chapter['forward'] = null;
					$this->chapter['index'] = null;
				}
			}
			// If book containt not headers
			else {
				$this->book['content'] = $this->content;
				$this->book['caption'] = null;
				$this->book['index'] = null;
				$this->book['back'] = null;
				$this->book['forward'] = null;
				$this->book['chapter'] = null;
			}
				
			// Book activity
			$this->activity($activity);
		}
		
		
		// Interactive activity navigation
		private function activity($activity) {
		
			if (empty($this->header) || $this->chapter['current'] == $this->chapter['total']) {
	
				$popup = 'onClick="popup(\'print.php?id='.$this->id.'\')"';

				$this->book['activity'] = "<ul>\n";

				foreach($activity as $key => $value) {
					$poplink = ($key == "print") ? $popup : null;
					$key = ($key == "print") ? "#" : $key.".php?id=".$this->id;
					$this->book['activity'] .= "<li><a href='".$key."' ".$poplink.">$value</a></li>\n";
				}

				$this->book['activity'] .= "</ul>\n";
			}
			else
				$this->book['activity'] = null; 
		}
		
		
		// Set content chapters
		private function setHeaders() {
			
			preg_match_all('/<h1>([^>]*)<\/h1>/', $this->content, $match);
			unset($match[0]);
	
			foreach ($match as $key => $array) {
	
				$array = array_unique($array);
	
				foreach ($array as $subkey => $header) {
					$i++;
					$this->header[$i] = $header;
				}
			}
		}
		
		
		// Book tree
		public function tree() {
			
			if (!empty($this->header)) {
				foreach ($this->header as $key => $header) {
					$tree['class'] = ($key == $this->chapter['current']) ? "current" : null;
					$tree['lead'] = "|chapter| ".$this->numberToRoman($key);
					$tree['title'] = $header;
					$tree['link'] = "content.php?id=".$this->id."&part=".$key;
					$return[] = $tree;
				}
			}
			
			return $return;
		}


		// Book parts
		public function bookParts() {

			$sql = mysql_query("SELECT book_id, book_title FROM book 
								WHERE book_volume = '".$this->book['book_volume']."' AND book_id != '".$this->id."'");

			if (mysql_num_rows($sql) > 0) {

				$sql = mysql_query("SELECT book_id, book_title FROM book WHERE book_volume = '".$this->book['book_volume']."' ORDER BY book_post_date DESC");
				
				$part = mysql_num_rows($sql);
				
				while ($row = mysql_fetch_assoc($sql)){
					
					$row['book_class'] = ($row['book_id'] == $this->id) ? "current" : null;
					$row['book_part'] = $this->numberToRoman($part);
					$bookparts[] = $row;
					$part--;
				}
			}

			return $bookparts;
		}


		// Author books
		public function fromAuthor() {

			$sql = mysql_query("SELECT book_id, book_volume, book_title FROM book
								WHERE book_author_id = ".$this->book['book_author_id']." AND  book_id != ".$this->id." AND book_approve = '0'
								ORDER BY book_counter DESC LIMIT 5");

			if (mysql_num_rows($sql) > 0) {

				while ($row = mysql_fetch_assoc($sql)) {
	
					$row['lead'] = null;
					$row['class'] = null;
					$row['title'] = "<b>".$row['book_volume']."</b>".$row['book_title'];
					$row['link'] = "book.php?id=".$row['book_id'];
					$return[] = $row;
				}
			}

			return $return;
		}


		// Read more books
		public function readMore() {

			// Ndaje fushen tags ne pjese
			$bookTags = explode(",",$this->book['book_tags']);

			// Numero fushen
			$count = count($bookTags);

			// Nese definohet ndonje tag
			if ($count > 0) {
				// MySQL Query Bilder
				$queryBilder = "SELECT book_id, book_author_id, book_volume, book_title FROM book WHERE ";
					// Query Bilder
					for ($i=0; $i<$count; $i++) {
						$trim = trim($bookTags[$i]);
						$queryBilder .= "book_tags LIKE '%$trim%' ";
						if ($i < ($count - 1)) $queryBilder .= "OR ";
					}
				$queryBilder .= "AND book_id <> '".$this->id."' AND book_approve = '0' ORDER BY book_counter DESC LIMIT 5";
				// MySQL Pyetsori
				$sql = mysql_query($queryBilder);
			}

			// Nese gjen ndonje resht ne arkive
			if (mysql_num_rows($sql) > 0) {

				while ($row = mysql_fetch_assoc($sql)) {

					if ($this->id != $row['book_id']) {
						$row['lead'] = implode(mysql_fetch_assoc(mysql_query("SELECT author_name FROM author WHERE author_id=".$row['book_author_id']."")));
						$row['class'] = null;
						$row['title'] = "<b>".$row['book_volume']."</b>".$row['book_title'];
						$row['link'] = "book.php?id=".$row['book_id'];
						$return[] = $row;
					}
				}
			}

			return $return;
		}
	}
?>